package com.dq.controller.user.centre;

import com.dq.controller.in.user.UserSummaryQuery;
import com.dq.domain.sys.user.user.BossUser;
import com.dq.domain.sys.user.user.centre.UserPlan;
import com.dq.domain.sys.user.user.centre.UserSummary;
import com.dq.exception.customException.AdminException;
import com.dq.service.AllService;
import com.dq.vo.ListDataVo;
import com.dq.vo.Vo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author luchangtong
 * @Date 2020/4/10 14:06
 */
@RestController
@RequestMapping("/sys/userSummary/")
@Api(description = "工作汇报管理")
public class SysUserSummaryController extends AllService {

    @PostMapping("list")
    @ApiOperation("工作计划")
    public Vo list(@RequestBody UserSummaryQuery userSummaryQuery) throws AdminException{
        userSummaryQuery.format();

        List<UserSummary.Vo> listVo = new ArrayList<>();
        List<UserSummary> list = userSummaryService.list(userSummaryQuery);
        for (UserSummary userSummary : list) {
            UserSummary.Vo vo = (UserSummary.Vo) userSummary.toVO();
            UserPlan get = userPlanRepository.findByDeletedFalseAndYearAndMonthAndBossUser(userSummary.getYear(), userSummary.getMonth(), (BossUser) userSummary.getCreateUser());
            if(get != null) {
                Map<String, Object> map = userSummaryService.completeMap(userSummary.getCreateUser().getId(), get.getTarget());
                vo.setTarget(get.getTarget());
                vo.setFinish(new BigDecimal(map.get("completedByMonth").toString()));
                vo.setResidue(new BigDecimal(map.get("incomplete").toString()));
            }

            listVo.add(vo);
        }

        return new ListDataVo(listVo, userSummaryService.count(userSummaryQuery));
    }
}
